package com.hrzyit.whistle.cases.ui.activity

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.util.Log
import android.view.View
import cn.bingoogolapple.refreshlayout.BGANormalRefreshViewHolder
import cn.bingoogolapple.refreshlayout.BGARefreshLayout
import com.alibaba.android.arouter.facade.annotation.Route
import com.hrzyit.base.common.BaseConstant
import com.hrzyit.base.data.protocol.ApiPageInfo
import com.hrzyit.base.ext.startLoading
import com.hrzyit.base.ui.activity.BaseMvpActivity
import com.hrzyit.base.ui.adapter.BaseRecyclerViewAdapter
import com.hrzyit.provider.routers.RouterPath
import com.hrzyit.whistle.cases.R
import com.hrzyit.whistle.cases.common.CaseConstant
import com.hrzyit.whistle.cases.data.bean.AuthMsgEntityVo
import com.hrzyit.whistle.cases.data.bean.CaseDetailInfoEntity
import com.hrzyit.whistle.cases.data.bean.CaseInfoEntity
import com.hrzyit.whistle.cases.injection.component.DaggerCaseCenterComponent
import com.hrzyit.whistle.cases.injection.module.CaseCenterModule
import com.hrzyit.whistle.cases.injection.module.CaseTypeModule
import com.hrzyit.whistle.cases.presenter.CaseListPresenter
import com.hrzyit.whistle.cases.presenter.view.CaseListView
import com.hrzyit.whistle.cases.ui.adapter.CaseAdapter
import com.kennyc.view.MultiStateView
import kotlinx.android.synthetic.main.activity_patrol_case.*
import org.jetbrains.anko.toast

/**
 * 网格案件界面
 * @author YangWei
 * @date 2019/3/26
 */
@Route(path = RouterPath.CasesCenter.PATH_PATROL_CASE)
class PatrolCaseActivity : BaseMvpActivity<CaseListPresenter>(),
    CaseListView, BGARefreshLayout.BGARefreshLayoutDelegate {


    // 案件数据列表适配器
    private lateinit var caseListAdapter: CaseAdapter

    private var mCurrentPage: Int = 1

    private var mMaxPage: Int = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_patrol_case)

        initView()
        initRefreshLayout()
        loadData()

    }

    /**
     * 初始化视图
     */
    private fun initView() {
        // 0. 获取案件列表控件：mCaseListRv
        // 1. 设置案件列表控件属性
        mPatrolCaseListRv.layoutManager = LinearLayoutManager(this) // 线性布局
        mPatrolCaseListRv.setHasFixedSize(true)
        // 2. 设置适配器
        caseListAdapter = CaseAdapter(this)
        mPatrolCaseListRv.adapter = caseListAdapter

        // 3. 配置适配器中组件的事件
        caseListAdapter.setOnItemClickListener(object : BaseRecyclerViewAdapter.OnItemClickListener<CaseInfoEntity> {

            override fun onItemClick(view: View, item: CaseInfoEntity?, position: Int) {
                when (view.id) {
                    R.id.mLeaderApproveIv -> {
                        // TODO 2019/4/7 - 待完成内容：弹出批示或查看批示容器
                    }
                    R.id.mCaseItemList -> {
                        // TODO 2019/4/7 - 待完成内容：跳转到案件详情页面
                    }
                }
            }
        })

    }

    /**
     * 初始化刷新视图
     */
    private fun initRefreshLayout() {
        mPatrolRefreshLayout.setDelegate(this)
        val viewHolder = BGANormalRefreshViewHolder(this, true)
        viewHolder.setLoadMoreBackgroundColorRes(R.color.common_bg)
        viewHolder.setRefreshViewBackgroundColorRes(R.color.common_bg)
        mPatrolRefreshLayout.setRefreshViewHolder(viewHolder)
    }

    /**
     * 加载数据
     */
    private fun loadData() {
        // 打开加载多状态视图-加载状态
        mPatrolMultiStateView.startLoading()
        mPresenter.selectCaseListByCaseType(CaseConstant.CASE_SORT_GRID, mCurrentPage, BaseConstant.PAGE_SIZE_DEFAULT)

    }

    /**
     * Dagger 注入
     */
    override fun injectComponent() {

        DaggerCaseCenterComponent.builder()
            .activityComponent(mActivityComponent)
            .caseTypeModule(CaseTypeModule())
            .caseCenterModule(CaseCenterModule())
            .build()
            .inject(this)

        // 初始化 Presenter.View
        mPresenter.mView = this
    }


    /**
     * 获取案件分类对应的案件数据列表分页回调
     */
    override fun onGetCaseInfoListSuccess(result: ApiPageInfo<CaseInfoEntity>) {
        //  关闭上拉、下拉动画
        mPatrolRefreshLayout.endLoadingMore()
        mPatrolRefreshLayout.endRefreshing()
        val list = result.records
        if (list != null && list.isNotEmpty()) {
            mMaxPage = result.pages
            if (mCurrentPage == 1) {
                caseListAdapter.setData(result.records!!)
            } else {
                caseListAdapter.dataList.addAll(result.records!!)
                caseListAdapter.notifyDataSetChanged()
            }
            // 多状态视图-内容显示状态
            mPatrolMultiStateView.viewState = MultiStateView.VIEW_STATE_CONTENT

        } else {
            // 多状态视图-无数据显示状态
            mPatrolMultiStateView.viewState = MultiStateView.VIEW_STATE_EMPTY
        }
    }

    override fun onGetCaseAuthInfoSuccess(result: AuthMsgEntityVo) {
    }

    override fun onGetSaveAuthMsgSuccess(result: Boolean) {
    }

    /**
     * 上拉加载更多
     */
    override fun onBGARefreshLayoutBeginLoadingMore(refreshLayout: BGARefreshLayout?): Boolean {
        return if (mCurrentPage < mMaxPage) {
            mCurrentPage++
            loadData()
            true
        } else {
            false
        }
    }

    /**
     * 下拉刷新:第一页
     */
    override fun onBGARefreshLayoutBeginRefreshing(refreshLayout: BGARefreshLayout?) {
        mCurrentPage = BaseConstant.PAGE_CURRENT_DEFAULT
        loadData()
    }
}
